<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/1/17
 * Time: 9:28
 */
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;
class Details extends Nocommon
{
    /*
     * 酒店预订须知
     */
    public function hotel()
    {
        $id = input('id', '', 'trim');
        $res = Db::name('hotel')->field('city,address,latest_time')->where('hotel_id', $id)->find();
        $this->assign('res', $res);
        return view();
    }

    /*
     * 景点订购须知
     */
    public function tour()
    {
        $id = input('id', '', 'trim');
        $res = Db::name('tours')->field('city,address,in_tour_time')->where('tour_id', $id)->find();
        $this->assign('res', $res);
        return view();
    }

    /*
     * 用户注册协议
     */
    public function register()
    {
        //查询
        $res = Db::name('agreement')->where('id', 2)->find();
        if ($res) {
            $this->assign('content', $res);
        }
        return $this->fetch();
    }

    /*
     * 隐私协议
     */
    public function privacy()
    {
        //查询
        $res = Db::name('agreement')->where('id', 1)->find();
        if ($res) {
            $this->assign('content', $res);
        }
        return $this->fetch();
    }

    /*
        跟团游预定须知
    */
    public function team()
    {
        $id = input('id', '', 'trim');
        $res = Db::name('team_default')->where('team_id', $id)->find();
        $this->assign('res', $res);
        return view();
    }

    public function problem()
    {
        $id = input('id', '', 'trim');

        //查询用户协议列表
        $data = Db::name('problem')->field("content")->where("id", $id)->find();
        if ($data) {
            $this->assign('content', $data);
        }
        return $this->fetch();
    }

    /*
     * 用户电子名片
     */
    public function usercard()
    {
        $id = input('id', '', 'trim');
        $uid = input('uid', '', 'trim');
        if (!empty($id) && !empty($uid)) {
            //查询数据
            $card = Db::name('my_card')->where('id', $id)->find();
            if ($card['type'] == 1) {
                $res = Db::name('tours')->field('city,address,tour_subject')->where('tour_name', $card['store_name'])->find();
                $address = $res['city'] . $res['address'];
                $content = $res['tour_subject'];
            }
            if ($card['type'] == 2) {
                $res = Db::name('hotel')->field('city,address,hotel_subject')->where('hotel_name', $card['store_name'])->find();
                $address = $res['city'] . $res['address'];
                $content = $res['hotel_subject'];
            }
            if ($card['type'] == 3) {
                $res = Db::name('food_store')->field('food_store_address,city,food_store_content')->where('food_store_name', $card['store_name'])->find();
                $address = $res['city'] . $res['food_store_address'];
                $content = $res['food_store_content'];
            }
            if ($card['type'] == 4) {
                $res = Db::name('team')->field('city,team_address')->where('team_name', $card['store_name'])->find();
                $address = $res['city'] . $res['team_address'];
                $content = '';
            }
            $this->assign('address', $address);
            $this->assign('content', $content);

            $image = Db::name('card_img')->where('uid', $uid)->find();
            $count = Db::name('card_img')->where('uid', $uid)->count();
            $this->assign('uid', $uid);
            $this->assign('card', $card);
            $this->assign('image', $image);
            $this->assign('count', $count);
        }
        return $this->fetch();
    }

    /*
     * 商户电子名片
     */
    public function merchantcard()
    {
        $status = input('status', '', 'trim');
        $id = input('id', '', 'trim');
        $uid = input('uid', '', 'trim');
        if (!empty($status) && !empty($id) && !empty($uid)) {
            if ($status == 1) {
                $res = Db::name('food_store')->field('food_store_name as name,food_tag as tag,food_store_tel as tel,food_store_address as address,city,food_store_content as content')
                    ->where('food_store_id', $id)
                    ->find();
                //查询照片
                $image = Db::name('food_store_img')->where('store_id', $id)->find();
                $count = Db::name('food_store_img')->where('store_id', $id)->count();
                $tag = explode(',', $res['tag']);
                $this->assign('tag', $tag);
            }
            if ($status == 2) {
                $res = Db::name('tours')
                    ->field('tour_name as name,tour_tag as tag,tour_tel as tel,address,city,tour_subject as content')
                    ->where('tour_id', $id)
                    ->find();
                //查询照片
                $image = Db::name('tour_img')->where('tour_id', $id)->find();
                $count = Db::name('tour_img')->where('tour_id', $id)->count();
                $tag = explode(',', $res['tag']);
                $this->assign('tag', $tag);
            }
            if ($status == 3) {
                $res = Db::name('team')
                    ->field('team_name as name,team_phone as tel,team_address as address,city')
                    ->where('team_id', $id)
                    ->find();
                //查询照片
                $image = Db::name('team_img')->where('team_id', $id)->find();
                $count = Db::name('team_img')->where('team_id', $id)->count();
                $res['content'] = '';
            }
            if ($status == 4) {
                $res = Db::name('hotel')
                    ->field('hotel_name as name,hotel_tag as tag,hotel_tel as tel,address,city,hotel_subject as content')
                    ->where('hotel_id', $id)
                    ->find();
                $tag = explode(',', $res['tag']);
                $image = Db::name('hotel_img')->where('hotel_id', $id)->find();
                $count = Db::name('hotel_img')->where('hotel_id', $id)->count();
                $this->assign('tag', $tag);
            }

            $this->assign('res', $res);

            $this->assign('status', $status);
            $this->assign('id', $id);

            //查询照片
            $this->assign('uid', $uid);
            $this->assign('image', $image);
            $this->assign('count', $count);
        }
        return $this->fetch();
    }

    /*
     * 详情捡漏规则
     */
    public function jlourule()
    {

        //查询用户协议列表
        $data = Db::name('jlou_rule')->field("content")->find();
        if ($data) {
            $this->assign('content', $data);
        }
        return $this->fetch();
    }

    /*
     * 捡漏商品详情
     */
    public function goods()
    {
        $type = input('type', '', 'trim');
        $id = input('id', '', 'trim');
        if ($type == 1) {
            $res = Db::name('jlou_goods')->field('content')->where('id', $id)->find();
        } else if ($type == 2) {
            $res = Db::name('jlou_goods')->field('t_content as content')->where('id', $id)->find();
        }
        $this->assign('content', $res);
        return view();
    }

    public function test()
    {
        return view();
    }

    public function bargainrule()
    {
        //查询用户协议列表
        $data = Db::name('bargain_rule')->field("content")->find();
        if ($data) {
            $this->assign('content', $data);
        }
        return $this->fetch();
    }

    /*
     * 查看图片ajax
     */
    public function image()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = Db::name('card_img')->field('image')->where('uid', $post['uid'])->select();
            if ($data) {
                foreach ($data as $key => $v) {
                    $res [] = $v;
                }
                return json(['code' => 0, 'data' => $res]);

            } else {
                return ['code' => 1, 'message' => '暂无图片'];
            }
        }
    }

    /*
     * merchatImage
     */
    public function merchatImage()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            if ($post['status'] == 1) {
                $data = Db::name('food_store_img')->field('img_path as image')->where('store_id', $post['id'])->select();
            }
            if ($post['status'] == 2) {
                $data = Db::name('tour_img')->field('img_path as image')->where('tour_id', $post['id'])->select();
            }
            if ($post['status'] == 3) {
                $data = Db::name('team_img')->field('img_path as image')->where('team_id', $post['id'])->select();
            }
            if ($post['status'] == 4) {
                $data = Db::name('hotel_img')->field('img_path as image')->where('hotel_id', $post['id'])->select();
            }
            if ($data) {
                foreach ($data as $key => $v) {
                    $res [] = $v;
                }
                return json(['code' => 0, 'data' => $res]);

            } else {
                return ['code' => 1, 'message' => '暂无图片'];
            }
        }
    }

    /*
     * 砍价商品详情
     */
    public function kj_goods()
    {
        $id = input('id', '', 'trim');
        if (empty($id)) {
            $this->error('商品ID为空');
        }
        //查询用户协议列表
        $data = Db::name('bargain_goods')->field("goods_content")->where("id", $id)->find();
        if ($data) {
            $this->assign('content', $data);
        }
        return $this->fetch();
    }

    /*
     * 砍价分享页面
     */
    public function bargain_share()
    {
        $id = input('id', '', 'trim');
        if ($id) {
            $this->assign('bargain', 'yslybargainID_' . $id);
        }
        return $this->fetch();
    }

    /*
    * 砍价分享页面
    */
    public function vote_share()
    {
        $id = input('id', '', 'trim');
        if ($id) {
            $this->assign('bargain', 'yslyvoteID_' . $id);
        }
        return $this->fetch();
    }

    /*
     * 攻略分享页面
     */
    public function circle_text()
    {
        $type = input('type', '', 'trim');

        $id = input('id', '', 'trim');
        if ($type == 1) {
            $result = Db::name('method')
                ->alias('a')
                ->field('a.title,u.user_icon,u.nickname,a.create_time,a.content')
                ->join('user u', 'a.uid=u.uid', 'left')
                ->where('mid', $id)
                ->find();
            $result['create_time'] = time_ago($result['create_time']);
            $result['content'] = json_decode($result['content'], true);
        }
        if ($type == 2) {
            $result = Db::name('friends_circle')
                ->alias('a')
                ->field('a.fc_title as title,a.fc_content,a.create_time,u.user_icon,u.nickname')
                ->join('user u', 'a.uid=u.uid', 'left')
                ->where(['a.is_del' => 0, 'a.is_display' => 0, 'f_id' => $id])
                ->find();

            $result['create_time'] = time_ago($result['create_time']);
            $result['content'] = json_decode($result['fc_content'], true);
        }

        $this->assign('result', $result);
        return view();
    }

    /*
     * 攻略视频分享页面
     */
    public function circle_video()
    {
        $id = input('id','','trim');
        $result = Db::name('friends_circle')
            ->alias('a')
            ->field('a.fc_title,a.video,a.create_time,u.user_icon,u.nickname,video_content,video_covers')
            ->join('user u', 'a.uid=u.uid', 'left')
            ->where(['a.is_del' => 0, 'a.is_display' => 0, 'f_id' => $id])
            ->find();

        $result['create_time'] = time_ago($result['create_time']);
        $this->assign('result', $result);
        return view();
    }
}